package com.easidea.tim.flex.remote.supply
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.supply.Sup;
	import com.easidea.tim.flex.bean.supply.SupplierDTO;
	import com.easidea.tim.flex.bean.supply.SupplierLocator;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.AppAlert;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	
	
	public class SupplierDao extends BaseDao
	{
		private var _supplierService:RemoteObject = new RemoteObject();
		private var _supplierId:int;
		[Bindable]private var supplierLocator:SupplierLocator = SupplierLocator.getInstance();
		private static var _instance:SupplierDao;
		
		public static function getInstance():SupplierDao{
			if( !_instance ){
				_instance = new SupplierDao();
				
			}
			return _instance;
		}
		public function SupplierDao() {
				_supplierService.channelSet = this.buildChannelSet();
				_supplierService.destination="supService";	
		}
		/**
		 * 调用spring方法:根据供应商名称等参数分页查询供应商列表
		 * @param name
		 * @param page
		 * 
		 */		
		public function getSupplierList(name:String,page:Page,status:String):void {			
			_supplierService.addEventListener(ResultEvent.RESULT, showSupplierList);
			_supplierService.addEventListener(FaultEvent.FAULT,_Fault);
			_supplierService.getSupplierListByContion(name,page,status);
		}
		/**
		 * 根据供应商名称等参数分页查询供应商列表回调函数
		 * @param e
		 * 
		 */			
		private function showSupplierList(e:ResultEvent):void {			 
			_supplierService.removeEventListener(ResultEvent.RESULT, showSupplierList);
			 if(e.result != null) {		
				var page:Page = e.result as Page;					 				 
				Env.dispatchEvent(new CustomerEvent("getSupplierListSuccess",page));				  									 
			 } 
		}
		public function getInitDataList(id:int):void {
			_supplierId = id;
			_supplierService.addEventListener(ResultEvent.RESULT, showInitDataList);
			_supplierService.addEventListener(FaultEvent.FAULT, _Fault);
			_supplierService.getInitDataList();
			
		}

		
		private function showInitDataList(e:ResultEvent):void {			 
			_supplierService.removeEventListener(ResultEvent.RESULT, showInitDataList);
			var result:ArrayCollection = e.result as ArrayCollection;
			 if(result != null) {
				var allSupplierTypes:ArrayCollection = new ArrayCollection();				
				for each(var supplierType:Object in result[0]) {
					var obj:Object = new Object();
					obj.label = supplierType.name;
					obj.data = supplierType.id;
					allSupplierTypes.addItem(obj);
				}
				supplierLocator.allSupplierTypes = allSupplierTypes;
			 } 
		}
		
		/**
		 * 调用spring方法:根据供应商id查询供应商对象，id为-1时表示新增操作，否则为编辑操作
		 * @param id
		 * 
		 */		
		public function getSupplierById(id:int):void {
			if (id == -1) {
				supplierLocator.supplier = new SupplierDTO();
				supplierLocator.supplier.id = -1;
				supplierLocator.supplier.status = 1;
				supplierLocator.supplier.typeId = -1;
			} else {
				_supplierService.addEventListener(ResultEvent.RESULT, showSupplier);
				_supplierService.addEventListener(FaultEvent.FAULT, _Fault);
				_supplierService.getSupplierDTOById(id);
			}
		}
		/**
		 * 根据供应商id查询供应商对象回调函数
		 * @param e
		 * 
		 */		
		private function showSupplier(e:ResultEvent):void {			 
			_supplierService.removeEventListener(ResultEvent.RESULT, showSupplier);
			 if(e.result != null) {
				var supplier:SupplierDTO = e.result as SupplierDTO;	
				supplierLocator.supplier = supplier;
			 } else {
			 	AppAlert.show("供应商不存在！","提示");			 	
			 }
		}
		/**
		 *调用spring方法:新增或编辑供应商保存 
		 * @param customer
		 * 
		 */		
		public function saveSupplier(supplier:Sup):void {
	
				_supplierService.addEventListener(ResultEvent.RESULT, saveSupplierSuccess);
				_supplierService.addEventListener(FaultEvent.FAULT, _Fault);
				_supplierService.saveOrUpdateSupplier(supplier);
		}
	   /**
	    *新增或编辑供应商保存回调函数 
	    * @param e
	    * 
	    */		
	   private function saveSupplierSuccess(e:ResultEvent):void {			 
			_supplierService.removeEventListener(ResultEvent.RESULT, saveSupplierSuccess);
			 if(e.result != null) {
				AppAlert.show("保存成功！","提示");
				Env.dispatchEvent(new CustomerEvent("saveSupplierSuccess"));
			 } 
		}
		/**
		 *调用spring方法:删除供应商
		 * @param customer
		 * 
		 */		
		public function deleteSupplier(id:String):void {
	
				_supplierService.addEventListener(ResultEvent.RESULT, deleteSupplierSuccess);
				_supplierService.addEventListener(FaultEvent.FAULT, _Fault);
				_supplierService.deleteSupplier(id);
		}
	   /**
	    * 删除供应商回调函数 
	    * @param e
	    * 
	    */		
	   private function deleteSupplierSuccess(e:ResultEvent):void {			 
			_supplierService.removeEventListener(ResultEvent.RESULT, deleteSupplierSuccess);
			 if(e.result != null) {
				AppAlert.show("删除成功！","提示");
				Env.dispatchEvent(new CustomerEvent("deleteSupplierSuccess"));
			 } 
		}
		
	}
}